package homework3

import scala.collection.immutable.List
import scala.collection.mutable.ListBuffer

object  UserLocationTimeDemo {

  val list = List(
    ("UserA", "LocationA", 8, 60),
    ("UserA", "LocationA", 9, 60),
    ("UserB", "LocationB", 10, 60),
    ("UserB", "LocationB", 11, 80)
  )
  def main(args: Array[String]): Unit = {

    var resultList = ListBuffer(list(0))
    list.foreach { item =>
      var isReduce = false
      for (i <- 0 until resultList.size) {
        val e = resultList(i)
        if (e._1 == item._1 && e._2 == item._2) {
          resultList.update(i, (e._1, e._2, Math.min(e._3, item._3), e._4 + item._4 ))
          isReduce = true
        }
      }
      if (!isReduce) {
        resultList.append(item)
      }
    }
   // val result = list.reduce((x, y) => if (x._1 == y._1 && x._2 == y._2) (x._1, x._2, Math.min(x._3, y._3), x._4 + y._4))

    println(resultList)

  }

}
